﻿#include "xplay.h"
#include "ui_xplay.h"
#include<QFileDialog>
#include<QDebug>
#include<QMessageBox>
#include<string>
#include"xdemuxthread.h"

static XDemuxThread dt ;

XPlay::XPlay(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::XPlay)
{
    ui->setupUi(this);

    dt.Start();
    startTimer(40);


}

XPlay::~XPlay()
{
    //delete ui;
    dt.Close();
}

void XPlay::timerEvent(QTimerEvent *e)
{
    if(isSliderPress) return;
    long long total = dt.totalMs;
    if(total>0){
        double pos =(double)dt.pts/(double)total;
        int v = ui->horizontalSlider->maximum() * pos;
        ui->horizontalSlider->setValue(v);
    }
}

void XPlay::resizeEvent(QResizeEvent *event)
{
    ui->video->resize(this->size());
    ui->horizontalSlider->resize(this->width()-200,ui->horizontalSlider->height());
    ui->horizontalSlider->move(150,this->height()-100);
    ui->Isplay->move(50,this->height()-105);
    ui->OpenFile->move(20,20);
}

void XPlay::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(isFullScreen()){
        this->showNormal();
    }
    else{
        this->showFullScreen();
    }
}

void XPlay::SetPause(bool isPause)
{
    if(isPause){
        ui->Isplay->setText("播放");
    }
    else{
        ui->Isplay->setText("暂停");
    }

}

void XPlay::on_OpenFile_clicked()
{
    //选择文件
    //dt.Clear();

    QString name = QFileDialog::getOpenFileName(this,"选择视频文件");
    this->setWindowTitle(name);
    // std::string nameStr = n.toStdString();
    // //const char* name = NULL;
    // const char* name =nameStr.c_str();
    // qDebug()<<name;
    // if(!name) return;

    //this->setWindowTitle(name);
    // const char * url1 = "rtmp://58.200.131.2:1935/livetv/hunantv";
    // const char* url2 = "D:/AV/1280x720.mp4";
    // const char* url3 = "D:/AV/1280x720_2.mp4";
    // const char* url4 = "D:/AV/test_flv.flv";
    if(!dt.Open(name.toLocal8Bit(),ui->video)){
        QMessageBox::information(0,"error","open file failed!");
        return;
    }
    qDebug()<<"xplay Open success";

    // dt.Start();
    SetPause(dt.isPause);

    return;
}


void XPlay::on_Isplay_clicked()
{
    bool isPause = !dt.isPause;
    SetPause(isPause);
    dt.SetPause(isPause);
}


void XPlay::on_horizontalSlider_sliderPressed()
{
    isSliderPress = true;
}


void XPlay::on_horizontalSlider_sliderReleased()
{
    isSliderPress = false;
    double pos = 0.0;
    pos = (double)ui->horizontalSlider->value()/(double)ui->horizontalSlider->maximum();
    dt.Seek(pos);
}

